package com.one.apartmentsystem.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.one.apartmentsystem.app.domain.ApartmentInfo;
import com.one.apartmentsystem.app.domain.ContractInfo;
import com.one.apartmentsystem.app.domain.HousesInfo;
import com.one.apartmentsystem.app.dto.PageDTO;
import com.one.apartmentsystem.app.mapper.ApartmentInfoMapper;
import com.one.apartmentsystem.app.mapper.ContractInfoMapper;
import com.one.apartmentsystem.app.mapper.HousesInfoMapper;
import com.one.apartmentsystem.app.service.HousesInfoService;
import com.one.apartmentsystem.app.vo.HouseDetailsVO;
import com.one.apartmentsystem.app.vo.PageResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author mpk16
 * @description 针对表【houses_info】的数据库操作Service实现
 * @createDate 2024-08-03 13:39:49
 */
@Service
@Slf4j
public class HousesInfoServiceImpl extends ServiceImpl<HousesInfoMapper, HousesInfo>
        implements HousesInfoService {

    @Autowired
    ApartmentInfoMapper apartmentInfoMapper;

    @Autowired
    ContractInfoMapper contractInfoMapper;

    @Autowired
    HousesInfoMapper housesInfoMapper;

    @Override
    public PageResultVO<HouseDetailsVO> pageList(PageDTO pageDTO) {
        PageResultVO<HouseDetailsVO> pageResultVO = new PageResultVO<>();
        List<HouseDetailsVO> list = housesInfoMapper.selectpageList(pageDTO);
        pageResultVO.setList(list);
        Integer l = contractInfoMapper.selectCount(new LambdaQueryWrapper<ContractInfo>().eq(ContractInfo::getStatus, "0"));
        pageResultVO.setTotal(l);
        return pageResultVO;
    }

    @Override
    public HouseDetailsVO getHouseDetail(String contractId) {

        ContractInfo contractInfo = contractInfoMapper.selectById(contractId);
        HousesInfo housesInfo = this.getById(contractInfo.getHouseId());
        log.info("contractInfo:{}", contractInfo);
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(housesInfo.getApartmentId());
        HouseDetailsVO houseDetailsVO = new HouseDetailsVO();
        //housesInfo复制到houseDetailsVO
        BeanUtils.copyProperties(housesInfo, houseDetailsVO);
        BeanUtils.copyProperties(contractInfo, houseDetailsVO);
        BeanUtils.copyProperties(apartmentInfo, houseDetailsVO);

        return houseDetailsVO;
    }

    @Override
    public List<HouseDetailsVO> getHouseByUserId(Long userId) {

        List<ContractInfo> contractInfos = contractInfoMapper.selectList(new LambdaQueryWrapper<ContractInfo>().eq(ContractInfo::getUserId, userId));
        ArrayList<HouseDetailsVO> houseDetailsVOS = null;
        if (contractInfos != null && !contractInfos.isEmpty()) {
            houseDetailsVOS = new ArrayList<>();
            for (ContractInfo contractInfo : contractInfos) {
                HouseDetailsVO houseDetailsVO = new HouseDetailsVO();
                //contractInfo.getStatus() == 1 表示已签约
                if (contractInfo.getStatus() == 1) {
                    HousesInfo housesInfo = this.getById(contractInfo.getHouseId());
                    BeanUtils.copyProperties(housesInfo, houseDetailsVO);
                    BeanUtils.copyProperties(contractInfo, houseDetailsVO);
                    houseDetailsVOS.add(houseDetailsVO);
                }
            }
        }

        return houseDetailsVOS;
    }
}




